package com.chen.nongansampling.handdayin;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.zyapi.pos.PosManager;
import android.zyapi.pos.PrinterDevice;
import android.zyapi.pos.interfaces.OnPrintEventListener;
import android.zyapi.pos.utils.BitmapTools;

import com.chen.nongansampling.R;

public class handdayincaozuo extends Activity {
    String[] array;
    public String url;
    public String id;
    private PrinterDevice mPrinter = null;
    private int jishu;
    public static boolean ISFIRSTPRINT = true;//判定是否为第一次打印
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handdayin);
        PosManager.get().init(getApplicationContext(),"A980");
        Intent intent =getIntent();
        Bundle songList = getIntent().getExtras();
        array=songList.getStringArray("list");
        url = songList.getString("url");
        id = songList.getString("id");
        initPrint();
    }
    private void initPrint() {
        mPrinter = PosManager.get().getPrinterDevice();
        mPrinter.setPrintEventListener(new OnPrintEventListener() {
            @Override
            public void onEvent(int i) {
                switch (i) {
                    case EVENT_UNKNOW:
                        Toast.makeText(handdayincaozuo.this, "位置错误", Toast.LENGTH_SHORT).show();
                        break;
                    case  EVENT_NO_PAPER:
                        Toast.makeText(handdayincaozuo.this, "打印机缺纸", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_PAPER_JAM:
                        Toast.makeText(handdayincaozuo.this, "打印机卡纸", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_PRINT_OK:
                        Toast.makeText(handdayincaozuo.this, "打印完成", Toast.LENGTH_SHORT).show();
                        //printMix();
                        break;
                    case EVENT_HIGH_TEMP:
                        Toast.makeText(handdayincaozuo.this, "打印机温度过高", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_LOW_TEMP:
                        Toast.makeText(handdayincaozuo.this, "打印机温度过低", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_CONNECTED:
                        Toast.makeText(handdayincaozuo.this, "打印机连接完成", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_CONNECT_FAILD:
                        Toast.makeText(handdayincaozuo.this, "打印机连接失败", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_STATE_OK:
                        Toast.makeText(handdayincaozuo.this, "打印机状态正常", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_CHECKED_BLACKFLAG:
                        Toast.makeText(handdayincaozuo.this, "检测到黑标", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_NO_CHECKED_BLACKFLAG:
                        Toast.makeText(handdayincaozuo.this, "未检测到黑标", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_TIMEOUT:
                        Toast.makeText(handdayincaozuo.this, "打印机响应超时", Toast.LENGTH_SHORT).show();
                        break;
                    case EVENT_PRINT_FAILD:
                        Toast.makeText(handdayincaozuo.this, "打印失败", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(handdayincaozuo.this, "打印失败", Toast.LENGTH_SHORT).show();
                        break;
                }

            }

            @Override
            public void onCheckBlack(int i) {

            }

            @Override
            public void onGetState(int i, int i1) {

            }


        }); // Set up print listening
        //必须初始化
        mPrinter.init();
    }
    public void handdayin(View view){
        PrinterDevice.TextData tData_head = mPrinter.new TextData();//构造TextData实例
        if(ISFIRSTPRINT == false)
        {
            tData_head.addParam("1B4B15");//退步
            tData_head.addParam("1B4B15");//退步
            tData_head.addParam("1B4B15");//退步


            mPrinter.addText(10,tData_head);
            }else {
            tData_head.addParam("1B4B15");//退步
            mPrinter.addText(10,tData_head);
             }
        PrinterDevice.TextData tDataEnter1 = mPrinter.new TextData();//构造TextData实例
        tDataEnter1.addParam("1B4A08");//多输出到撕纸口(print more paper for paper tearing)
        tDataEnter1.addParam("1B4A08");
        tDataEnter1.addParam("1B4A08");
        mPrinter.addText(10,tDataEnter1);//添加到打印队列(add to print queue)
        String [] riqi=array[1].split(" ");
        BitmapPrinter bpImage = new BitmapPrinter(this);
        bpImage.setBitmapSize(500,250);
        bpImage.addText("食安采样单",19,150,20);
        bpImage.addText(" 样品编号："+id,19,0,40);
        bpImage.addText(" 市场名称："+array[4],19,0,65);
        bpImage.addText(" 样品名称："+array[0],19,0,90);
        bpImage.addText(" 采样日期："+riqi[0],19,0,115);
        bpImage.addText(" 采样时间："+riqi[1],19,0,140);
        bpImage.addText(" 经营户："+array[2],19,0,165);
        bpImage.addText(" 摊位号："+array[3],19,0,190);
        Bitmap mBitmap = BarcodeCreater.encode2dAsBitmap(url, 170, 170, 2);
        //Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(url, 200 , 160, "UTF-8", "M", "1", Color.BLACK, Color.WHITE, null, 0, null);

        System.out.println(url);
        bpImage.addQrcode(mBitmap,230,25);
        Bitmap bmp = bpImage.getBitmap();
        bmp = BitmapTools.gray2Binary(bmp);//对图像二值化
        // /*try{ Thread.sleep(200); }catch (Exception e ){ }*/
        byte[] printData =BitmapTools.bitmap2PrinterBytes(bmp);
        mPrinter.addBmp(10, 0, bmp.getWidth(), bmp.getHeight(), printData);
         PrinterDevice.TextData tDataEnter = mPrinter.new TextData();//构造TextData实例
            tDataEnter.addParam("1B4A08");//多输出到撕纸口(print more paper for paper tearing)
            tDataEnter.addParam("1B4A08");
            tDataEnter.addParam("1B4A08");
            mPrinter.addText(10,tDataEnter);//添加到打印队列(add to print queue)
         mPrinter.printStart();//开始队列打印(begin to print)
         bmp.recycle();
         ISFIRSTPRINT = false;//重置首次打印变量值(reset the first printing variable) }

//        String [] riqi=array[1].split(" ");
//        StringBuilder sb = new StringBuilder();
//        mPrinter.addText(50, "            食安采样单\n");//添加到打印队列
//       sb.append( " 样品编号："+id+"\n");
//       sb.append( " 市场名称："+array[4]+"\n");
//       sb.append( " 样品名称："+array[0]+"\n");
//       sb.append( " 采样日期："+riqi[0]+"\n");
//       //sb.append( " 采样时间："+riqi[1]+"\n");
//       sb.append( " 经营户："+array[2]+"\n");
//       sb.append( " 摊位号："+array[3]+"\n");
//
//        PrinterDevice.TextData tData = mPrinter.new TextData();//构造TextData实例
//        tData.addText(sb.toString());//添加打印内容
//        tData.addParam(PrinterDevice.PARAM_TEXTSIZE_1X);
//        mPrinter.addText(15, tData);//添加到打印队列
//        int mWidth_QR = 200;
//        int mHeight_QR = 200;
//        Bitmap mBitmap = BarcodeCreater.encode2dAsBitmap(url, mWidth_QR, mHeight_QR, 2);
//        mPrinter.addBmp(40, 250, mBitmap);





    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==133)//手持机Fn的键值(PDA keycode)[其它键值为：SCAN:280 BACK:13 LEFT:131 RIGHT:132]
        {
            ISFIRSTPRINT = true;

            //1->走纸检测   mWidth 黑标的宽度
            mPrinter.addAction(PrinterDevice.PRINTER_CMD_KEY_CHECKBLACK);
            mPrinter.printStart();
        }
        return super.onKeyDown(keyCode, event);
    }
    public void chongzhi(View view){
        if (mPrinter != null) {
            mPrinter.close();
        }
        PosManager.get().close();
       finish();
    }
    public void fanhuilist(View view){
//        Intent intent= new Intent();
//        intent.setClass(handdayincaozuo.this, newDayin.class);
//        intent.putExtra("list", array);
//        intent.putExtra("url", url);
//        intent.putExtra("id", id);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        handdayincaozuo.this.startActivity(intent);
//        startActivity(intent);
        onBackPressed();
    }

}
